﻿namespace QLPhongKham.BaseClass.BenhNhan
{
    using ConfigDatabase.Class;
    using Npgsql;
    using NpgsqlTypes;
    using System;
    using System.Data;

    public class dtoBNChiDinh
    {
        private ConnectDatabase _condata;
        private NpgsqlConnection _connect;
        private decimal _dedongia;
        private decimal _deid;
        private decimal _deidql;
        private decimal _deidvp;
        private int _desldathanhtoan;
        private int _desoluong;
        private int _idadongtien;
        private int _idathuchien;
        private int _iiddangnhap;
        private int _iiddoituong;
        private int _iidphong;
        private string _sghichu;
        private string _sidttvp;
        private string _smabn;
        private string _sngay;
        private string _sSchema;
        private string _stenmaytinh;

        public dtoBNChiDinh()
        {
            this.KhoiTao();
            this._condata = new ConnectDatabase();
            this._sSchema = this._condata.Schema;
        }

        public dtoBNChiDinh(ConnectDatabase conn)
        {
            this.KhoiTao();
            this._condata = conn;
            this._sSchema = this._condata.Schema;
        }

        public bool Delete()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Delete(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "bn_chidinh.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Delete(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { "delete from ", this._sSchema, ".", tableName, " where ", columnID, " = ", this._deid }), connect) {
                    CommandType = CommandType.Text
                };
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "bn_chidinh.cs");
                return false;
            }
        }

        public int GetIDInsert()
        {
            int iDInsert;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                iDInsert = this.GetIDInsert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert()", "bn_chidinh.cs");
                iDInsert = 1;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return iDInsert;
        }

        public int GetIDInsert(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select max(id) as idmax  from " + this._sSchema + "." + tableName + " ", connect).Fill(dataTable);
                int num = 0;
                try
                {
                    num = int.Parse(dataTable.Rows[0][0].ToString());
                }
                catch
                {
                }
                return ++num;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert(NpgsqlConnection connect)", "bn_chidinh.cs");
                return 1;
            }
        }

        public bool Insert()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Insert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "bn_chidinh.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Insert(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "insert into ", this._sSchema, ".", tableName, "(", columnID, ",", columnIDQL, ",", columnMABN, ",", columnNGAY, ",", columnIDDOITUONG, ",", columnIDPHONG, 
                    ",", columnIDVP, ",", columnSOLUONG, ",", columnDONGIA, ",", columnDADONGTIEN, ",", columnDATHUCHIEN, ",", columnGHICHU, ",", columnIDTTVP, ",", columnSLDATHANHTOAN, 
                    ",", columnIDDANGNHAP, ",", columnTENMAYTINH, ") values(", this._deid, ",", this._deidql, ",'", this._smabn, "',to_timestamp('", this._sngay, "','dd/mm/yyyy hh24:mi'),", this._iiddoituong, ",", this._iidphong, 
                    ",", this._deidvp, ",:", columnSOLUONG, ",:", columnDONGIA, ",", this._idadongtien, ",", this._idathuchien, ",:", columnGHICHU, ",'", this._sidttvp, "',:", columnSLDATHANHTOAN, 
                    ",", this._iiddangnhap, ",:", columnTENMAYTINH, ")"
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnGHICHU, NpgsqlDbType.Text, 200).Value = this._sghichu;
                command.Parameters.Add(columnTENMAYTINH, NpgsqlDbType.Text, 200).Value = this._stenmaytinh;
                command.Parameters.Add(columnDONGIA, NpgsqlDbType.Numeric, 15).Value = this._dedongia;
                command.Parameters.Add(columnSOLUONG, NpgsqlDbType.Integer, 15).Value = this._desoluong;
                command.Parameters.Add(columnSLDATHANHTOAN, NpgsqlDbType.Integer, 15).Value = this._desldathanhtoan;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "bn_chidinh.cs");
                return false;
            }
        }

        private void KhoiTao()
        {
            this._deid = 0M;
            this._deidql = 0M;
            this._smabn = "";
            this._sngay = "";
            this._iiddoituong = 0;
            this._iidphong = 0;
            this._deidvp = 0M;
            this._desoluong = 0;
            this._dedongia = 0M;
            this._idadongtien = 0;
            this._idathuchien = 0;
            this._sghichu = "";
            this._sidttvp = "";
            this._desldathanhtoan = 0;
            this._iiddangnhap = 0;
            this._stenmaytinh = Environment.MachineName;
        }

        public void Load()
        {
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                this.Load(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load()", "bn_chidinh.cs");
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
        }

        public void Load(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(string.Concat(new object[] { 
                    " select  ", columnID, ",", columnIDQL, ",", columnMABN, ",to_char(", columnNGAY, ",'dd/mm/yyyy hh24:mi') as ", columnNGAY, ",", columnIDDOITUONG, ",", columnIDPHONG, ",", columnIDVP, 
                    ",", columnSOLUONG, ",", columnDONGIA, ",", columnDADONGTIEN, ",", columnDATHUCHIEN, ",", columnGHICHU, ",", columnIDTTVP, ",", columnSLDATHANHTOAN, ",", columnIDDANGNHAP, 
                    ",", columnTENMAYTINH, " from ", this._sSchema, ".", tableName, " where ", columnID, " = ", this._deid
                 }), connect).Fill(dataTable);
                if (dataTable.Rows.Count != 0)
                {
                    this._deid = decimal.Parse(dataTable.Rows[0][columnID].ToString());
                    this._deidql = decimal.Parse(dataTable.Rows[0][columnIDQL].ToString());
                    this._smabn = dataTable.Rows[0][columnMABN].ToString();
                    this._sngay = dataTable.Rows[0][columnNGAY].ToString();
                    this._iiddoituong = int.Parse(dataTable.Rows[0][columnIDDOITUONG].ToString());
                    this._iidphong = int.Parse(dataTable.Rows[0][columnIDPHONG].ToString());
                    this._deidvp = decimal.Parse(dataTable.Rows[0][columnIDVP].ToString());
                    this._desoluong = int.Parse(dataTable.Rows[0][columnSOLUONG].ToString());
                    this._dedongia = decimal.Parse(dataTable.Rows[0][columnDONGIA].ToString());
                    this._idadongtien = int.Parse(dataTable.Rows[0][columnDADONGTIEN].ToString());
                    this._idathuchien = int.Parse(dataTable.Rows[0][columnDATHUCHIEN].ToString());
                    this._sghichu = dataTable.Rows[0][columnGHICHU].ToString();
                    this._sidttvp = dataTable.Rows[0][columnIDTTVP].ToString();
                    this._desldathanhtoan = int.Parse(dataTable.Rows[0][columnSLDATHANHTOAN].ToString());
                    this._iiddangnhap = int.Parse(dataTable.Rows[0][columnIDDANGNHAP].ToString());
                    this._stenmaytinh = dataTable.Rows[0][columnTENMAYTINH].ToString();
                }
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load(NpgsqlConnection connect)", "bn_chidinh.cs");
            }
        }

        public DataTable LoadAll()
        {
            DataTable table;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                table = this.LoadAll(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll()", "bn_chidinh.cs");
                table = null;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return table;
        }

        public DataTable LoadAll(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select  " + columnID + "," + columnIDQL + "," + columnMABN + ",to_char(" + columnNGAY + ",'dd/mm/yyyy hh24:mi') as " + columnNGAY + "," + columnIDDOITUONG + "," + columnIDPHONG + "," + columnIDVP + "," + columnSOLUONG + "," + columnDONGIA + "," + columnDADONGTIEN + "," + columnDATHUCHIEN + "," + columnGHICHU + "," + columnIDTTVP + "," + columnSLDATHANHTOAN + "," + columnIDDANGNHAP + "," + columnTENMAYTINH + " from " + this._sSchema + "." + tableName + " ", connect).Fill(dataTable);
                return dataTable;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll(NpgsqlConnection connect)", "bn_chidinh.cs");
                return null;
            }
        }

        public bool Update()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Update(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "bn_chidinh.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Update(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "update ", this._sSchema, ".", tableName, " set ", columnIDQL, " = ", this._deidql, ",", columnMABN, " = '", this._smabn, "',", columnNGAY, " = to_timestamp('", this._sngay, 
                    "','dd/mm/yyyy hh24:mi'),", columnIDDOITUONG, " = ", this._iiddoituong, ",", columnIDPHONG, " = ", this._iidphong, ",", columnIDVP, " = ", this._deidvp, ",", columnSOLUONG, " = :", columnSOLUONG, 
                    ",", columnDONGIA, " = :", columnDONGIA, ",", columnDADONGTIEN, " = ", this._idadongtien, ",", columnDATHUCHIEN, " = ", this._idathuchien, ",", columnGHICHU, " = :", columnGHICHU, 
                    ",", columnIDTTVP, " = '", this._sidttvp, "',", columnSLDATHANHTOAN, " = :", columnSLDATHANHTOAN, ",", columnIDDANGNHAP, " = ", this._iiddangnhap, ",", columnTENMAYTINH, " =:", columnTENMAYTINH, 
                    " where ", columnID, " = ", this._deid
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnGHICHU, NpgsqlDbType.Text, 200).Value = this._sghichu;
                command.Parameters.Add(columnTENMAYTINH, NpgsqlDbType.Text, 200).Value = this._stenmaytinh;
                command.Parameters.Add(columnDONGIA, NpgsqlDbType.Numeric, 15).Value = this._dedongia;
                command.Parameters.Add(columnSOLUONG, NpgsqlDbType.Integer, 15).Value = this._desoluong;
                command.Parameters.Add(columnSLDATHANHTOAN, NpgsqlDbType.Integer, 15).Value = this._desldathanhtoan;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "bn_chidinh.cs");
                return false;
            }
        }

        public static string columnDADONGTIEN
        {
            get
            {
                return "dadongtien";
            }
        }

        public static string columnDATHUCHIEN
        {
            get
            {
                return "dathuchien";
            }
        }

        public static string columnDONGIA
        {
            get
            {
                return "dongia";
            }
        }

        public static string columnGHICHU
        {
            get
            {
                return "ghichu";
            }
        }

        public static string columnID
        {
            get
            {
                return "id";
            }
        }

        public static string columnIDDANGNHAP
        {
            get
            {
                return "iddangnhap";
            }
        }

        public static string columnIDDOITUONG
        {
            get
            {
                return "iddoituong";
            }
        }

        public static string columnIDPHONG
        {
            get
            {
                return "idphong";
            }
        }

        public static string columnIDQL
        {
            get
            {
                return "idql";
            }
        }

        public static string columnIDTTVP
        {
            get
            {
                return "idttvp";
            }
        }

        public static string columnIDVP
        {
            get
            {
                return "idvp";
            }
        }

        public static string columnMABN
        {
            get
            {
                return "mabn";
            }
        }

        public static string columnNGAY
        {
            get
            {
                return "ngay";
            }
        }

        public static string columnSLDATHANHTOAN
        {
            get
            {
                return "sldathanhtoan";
            }
        }

        public static string columnSOLUONG
        {
            get
            {
                return "soluong";
            }
        }

        public static string columnTENMAYTINH
        {
            get
            {
                return "tenmaytinh";
            }
        }

        public int pDADONGTIEN
        {
            get
            {
                return this._idadongtien;
            }
            set
            {
                this._idadongtien = value;
            }
        }

        public int pDATHUCHIEN
        {
            get
            {
                return this._idathuchien;
            }
            set
            {
                this._idathuchien = value;
            }
        }

        public decimal pDONGIA
        {
            get
            {
                return this._dedongia;
            }
            set
            {
                this._dedongia = value;
            }
        }

        public string pGHICHU
        {
            get
            {
                return this._sghichu;
            }
            set
            {
                this._sghichu = value;
            }
        }

        public decimal pID
        {
            get
            {
                return this._deid;
            }
            set
            {
                this._deid = value;
            }
        }

        public int pIDDANGNHAP
        {
            get
            {
                return this._iiddangnhap;
            }
            set
            {
                this._iiddangnhap = value;
            }
        }

        public int pIDDOITUONG
        {
            get
            {
                return this._iiddoituong;
            }
            set
            {
                this._iiddoituong = value;
            }
        }

        public int pIDPHONG
        {
            get
            {
                return this._iidphong;
            }
            set
            {
                this._iidphong = value;
            }
        }

        public decimal pIDQL
        {
            get
            {
                return this._deidql;
            }
            set
            {
                this._deidql = value;
            }
        }

        public string pIDTTVP
        {
            get
            {
                return this._sidttvp;
            }
            set
            {
                this._sidttvp = value;
            }
        }

        public decimal pIDVP
        {
            get
            {
                return this._deidvp;
            }
            set
            {
                this._deidvp = value;
            }
        }

        public string pMABN
        {
            get
            {
                return this._smabn;
            }
            set
            {
                this._smabn = value;
            }
        }

        public string pNGAY
        {
            get
            {
                return this._sngay;
            }
            set
            {
                this._sngay = value;
            }
        }

        public int pSLDATHANHTOAN
        {
            get
            {
                return this._desldathanhtoan;
            }
            set
            {
                this._desldathanhtoan = value;
            }
        }

        public int pSOLUONG
        {
            get
            {
                return this._desoluong;
            }
            set
            {
                this._desoluong = value;
            }
        }

        public string pTENMAYTINH
        {
            get
            {
                return this._stenmaytinh;
            }
            set
            {
                this._stenmaytinh = value;
            }
        }

        public static string tableName
        {
            get
            {
                return "bn_chidinh";
            }
        }
    }
}

